package businesslogic.frame;

import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import po.framepo.TermRange;

import businesslogicservice.frameblservice.ShowFrameBLService;
import useDatabase.java.getTable;
import useDatabaseService.GetTableImpli;
import vo.publishframevo.FrameVO;
import Enum.CourseNecessity;
import Enum.Module;
import Enum.ResultMessage;

public class ShowFrame  implements ShowFrameBLService{
	@Override
	public ResultMessage getInfo() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<FrameVO> getFrame() throws RemoteException {
		// TODO Auto-generated method stub
		DefaultTableModel tableModel = null;
		GetTableImpli getTable = new getTable();
		tableModel = getTable
				.getTable(
						"frame",
						"module,courseNecessity,courseClassification,courseName,credit,term",
						"");
		ArrayList<FrameVO> resultFrame = new ArrayList<FrameVO>();
		for (int i = 0; i < tableModel.getRowCount(); i++) {
			Module module = Module.valueOf(tableModel.getValueAt(i, 0)
					.toString());
			CourseNecessity courseNecessity = CourseNecessity
					.valueOf(tableModel.getValueAt(i, 1).toString());
			String courseClassification = tableModel.getValueAt(i, 2)
					.toString();
			String courseName = tableModel.getValueAt(i, 3).toString();

			int credit = Integer.parseInt(tableModel.getValueAt(i, 4)
					.toString());
			int start=Integer.parseInt((tableModel
					.getValueAt(i, 5).toString()).split(";")[0]);
			int end=Integer.parseInt((tableModel
					.getValueAt(i, 5).toString()).split(";")[1]);
			TermRange termRange = new TermRange(start, end);
			FrameVO frameVO = new FrameVO(courseName, credit, termRange,courseNecessity, courseClassification, module);
			resultFrame.add(frameVO);
		}
		return resultFrame;
	}

	@Override
	public ArrayList<FrameVO> getFrame(Module module) throws RemoteException {
		// TODO Auto-generated method stub
		GetTableImpli getTable=new getTable();
		DefaultTableModel tableModel=getTable.getTable("frame", "module,courseNecessity,courseClassification,courseName,credit,term",
				"module='"+module.toString()+"'");
		ArrayList<FrameVO> resultFrame = new ArrayList<FrameVO>();
		for (int i = 0; i < tableModel.getRowCount(); i++) {
			CourseNecessity courseNecessity = CourseNecessity
					.valueOf(tableModel.getValueAt(i, 1).toString());
			String courseClassification = tableModel.getValueAt(i, 2)
					.toString();
			String courseName = tableModel.getValueAt(i, 3).toString();

			int credit = Integer.parseInt(tableModel.getValueAt(i, 4)
					.toString());
			int start=Integer.parseInt((tableModel
					.getValueAt(i, 5).toString()).split(";")[0]);
			int end=Integer.parseInt((tableModel
					.getValueAt(i, 5).toString()).split(";")[1]);
			TermRange termRange = new TermRange(start, end);
			FrameVO frameVO = new FrameVO(courseName, credit, termRange,courseNecessity, courseClassification, module);
			resultFrame.add(frameVO);
		}
		return resultFrame;
	}
	
	
	

}
